﻿using System;
using System.Collections.Generic;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Hangfire;
using Hangfire.Autofac;
using Hangfire.Dashboard;
using Hangfire.Logging;
using Hangfire.Logging.LogProviders;
using Hangfire.RecurringJobExtensions;
using Hangfire.Redis;
using Identification.Importer.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Identification.Importer.Server
{
    public class Startup
    {
        private IContainer _container { get; set; }

        private readonly IConfiguration _configuration;

        public Startup(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        // ConfigureServices is where you register dependencies. This gets
        // called by the runtime before the ConfigureContainer method, below.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //Add Hangfire
            services.AddHangfire(configuration => { });

            // Create the container builder.
            var builder = new ContainerBuilder();
            builder.Populate(services);
            builder.RegisterModule(new ServiceDependencyModule());

            _container = builder.Build();

            //Create the IServiceProvider based on the container.
            return new AutofacServiceProvider(_container);
        }

        // Configure is where you add middleware. This is called after
        // ConfigureContainer. You can use IApplicationBuilder.ApplicationServices
        // here if you need to resolve things from the container.
        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, ServiceSetting setting)
        {
            loggerFactory.CreateLogger<Startup>().LogInformation("配置成功了");

            LogProvider.SetCurrentLogProvider(new SerilogLogProvider());
            GlobalConfiguration.Configuration.UseAutofacActivator(_container);
            GlobalConfiguration.Configuration.UseRedisStorage(setting.RedisServer,
                new RedisStorageOptions
                {
                    Db = setting.RedisDbIndex,
                    Prefix = setting.RedisPrefix,
                    FetchTimeout = TimeSpan.FromSeconds(setting.RedisFetchTimeoutSecond)
                });

            app.UseHangfireDashboard("", new DashboardOptions
            {
                AppPath = "/",
                Authorization = new List<IDashboardAuthorizationFilter>()
            });

            CronJob.AddOrUpdate(setting.HangfireCronJobFile);

            app.UseHangfireServer(new BackgroundJobServerOptions
            {
                ServerName = Environment.MachineName,
                HeartbeatInterval = TimeSpan.FromSeconds(30),
                ServerCheckInterval = TimeSpan.FromSeconds(30),
                Queues = setting.HangfireServerQueues.ToArray()
            });
        }
    }
}